package com.at.atapp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.at.appmodel.AlarmManagerModel;

public class AlarmManager extends Activity {
	
	private AlarmManagerModel oAlarmManagerModel;
	private ListView lv;
	private Vibrator v;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_alarm_manager);
	    
	    oAlarmManagerModel =  AlarmManagerModel.getInstance(getBaseContext());
	    
	    // TODO Auto-generated method stub
	    v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	    
	    lv = (ListView) findViewById(R.id.listview_vibTones);
	    lv.setOnItemClickListener(listviewhandler);
	    
        // Instanciating an array list (you don't need to do this, you already have yours)
        ArrayList<String> your_array_list = new ArrayList<String>();
        
        Set<String> keyset	= oAlarmManagerModel.getVibTones().keySet();
        Iterator<String> itr = keyset.iterator();
        
        while(itr.hasNext()){

        	your_array_list.add(itr.next());
        }
        // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
        ArrayAdapter<String> arrayAdapter =      
        new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
        lv.setAdapter(arrayAdapter); 	    
	}

	OnItemClickListener	listviewhandler	=	new AdapterView.OnItemClickListener() 
	{
	 public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) 
	 {
	    //Here write your code for starting the new activity on selection of list item
		 Toast.makeText(getApplicationContext(), ((TextView) childView).getText(),
		          Toast.LENGTH_SHORT).show();
		 AlarmManager.this.v.vibrate(oAlarmManagerModel.getVibTones().get(((TextView) childView).getText()),-1);
		 oAlarmManagerModel.setVibration(((TextView) childView).getText().toString());
		 Log.w("LVIEW","LVIEW" + position);
	 }
 
	};

	@Override
	protected void onPause() {
			super.onPause();
		    // The SharedPreferences editor - must use commit() to submit changes
			oAlarmManagerModel.saveModel();
	}
}
